package com.css.fxfzfxqh.modules.reportmanagement.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzfxqh.base.response.RestResponse;
import com.css.fxfzfxqh.base.user.viewobjects.UserQueryParams;
import com.css.fxfzfxqh.modules.lifelineEngineering.entity.LifelineTaskEntity;
import com.css.fxfzfxqh.modules.reportmanagement.service.LifelineReportService;
import com.css.fxfzfxqh.modules.reportmanagement.service.impl.ExportReportDataServiceImpl;
import com.css.fxfzfxqh.modules.reportmanagement.vo.CurRole;
import com.css.fxfzfxqh.modules.reportmanagement.vo.LifelineReportVo;
import com.css.fxfzfxqh.util.PlatformPageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;

@Slf4j
@RestController
@RequestMapping("/lifelineReport")
public class LifelineReportController {

    @Autowired
    private LifelineReportService lifelineReportService;
    @Autowired
    private ExportReportDataServiceImpl exportReportDataServiceImpl;

    @GetMapping("/getAll")
    public RestResponse<JSONObject> getAll(HttpServletRequest request, LifelineReportVo vo) {
        RestResponse restResponse = null;
        try{
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            vo.setCurPage(curPage);
            vo.setPageSize(pageSize);
            restResponse = RestResponse.succeed(lifelineReportService.getAll(vo));
        }catch (Exception e){
            log.error("查询用户失败!",e);
            restResponse = RestResponse.fail("查询用户失败");
        }
        return restResponse;
    }

    @GetMapping("/getCurRole")
    public RestResponse<JSONObject> getRole() {
        RestResponse restResponse = null;
        try{
            CurRole curRole = lifelineReportService.getCurRole();
            restResponse = RestResponse.succeed(curRole);
        } catch (RuntimeException e){
            log.error("查询用户权限失败!",e);
            restResponse = RestResponse.fail(e.getMessage());
        }catch (Exception e){
            log.error("查询用户失败!",e);
            restResponse = RestResponse.fail("查询用户失败");
        }
        return restResponse;
    }

    //生成报告
    @PostMapping("/createReport/{id}")
    public RestResponse createReport(HttpServletResponse response ,@PathVariable String id){
        RestResponse restResponse = null;
        try {
            LifelineTaskEntity entity = lifelineReportService.createReport(response, id);
            restResponse = RestResponse.succeed(entity);
        } catch (RuntimeException e) {
            log.error(e.getMessage(),e);
            restResponse = RestResponse.fail(e.getMessage());
        } catch (Exception e) {
            String errormessage = "生成报告失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        System.out.println("报告生成完成");
        return restResponse;
    }

    //上传报告
    @PostMapping("/uploadReport")
    public RestResponse uploadReport(@RequestBody LifelineTaskEntity entity){
        RestResponse restResponse = null;
        try {
            lifelineReportService.uploadReport(entity);
            restResponse = RestResponse.succeed(entity);
        } catch (Exception e) {
            String errormessage = "上传报告失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    //上传报告
    @GetMapping("/getPicture")
    public RestResponse getPicture(){
        RestResponse restResponse = null;
        try {
            HashMap<String, Object> map = new HashMap<>();
            exportReportDataServiceImpl.getReportPicture(map,"143227c3735b412ebbcf3fee7968f773",1);
            restResponse = RestResponse.succeed(map);
        } catch (Exception e) {
            String errormessage = "上传报告失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }
}
